package com.yipao.db.service.order;

import com.yipao.db.dao.order.OrderStatisticsMapper;
import com.yipao.db.domain.order.vo.DayTypeConstant;
import com.yipao.db.domain.order.vo.OrderAnalysisVo;
import com.yipao.db.domain.order.vo.OrderRankVo;
import com.yipao.db.util.OrderUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

/**
 * 订单统计
 *
 * @author tianhao
 * @date 2020/5/2 17:53
 **/
@Service
public class OrderStatisticsService {
    @Resource
    private OrderStatisticsMapper orderStatisticsMapper;

    /**
     * 1. 统计商品的销售量和销售金额
     * 2. 销售占比
     * 3. 销售额占比
     */
    public void orderGoodsAmountNumber() {
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate localDate = LocalDate.now();
        String dayStartTime = fmt.format(localDate.minusDays(1)) + " 00:00:00";
        String monthStartTime = fmt.format(localDate.minusMonths(1)) + " 00:00:00";
        String endTime = fmt.format(localDate);
        Integer payStatus = Integer.valueOf(OrderUtil.PAT_STATUS_Y);
        // 今日销售量
        int dayNumber = orderStatisticsMapper.querySumOrderGoodsNumberByTime(dayStartTime, endTime, payStatus);
        // 本月销售量
        int monthNumber = orderStatisticsMapper.querySumOrderGoodsNumberByTime(monthStartTime, endTime, payStatus);

        // 今日销售金额
        BigDecimal dayAmount = orderStatisticsMapper.querySumOrderSaleAmountByTime(dayStartTime, endTime, payStatus);
        // 本月销售金额
        BigDecimal monthAmount = orderStatisticsMapper.querySumOrderSaleAmountByTime(monthStartTime, endTime, payStatus);
        if (monthNumber > 0) {
            // 销售量占比
            orderStatisticsMapper.queryOrderGoodsNumberScaleByTime(monthNumber, monthStartTime, endTime, payStatus);
        }
        if (monthAmount.compareTo(BigDecimal.ZERO) > 0) {
            // 销售额占比
            orderStatisticsMapper.queryOrderSaleAmountScaleByTime(monthAmount, monthStartTime, endTime, payStatus);
        }
    }

    /***
     * 销售排行,前10
     * @param dayType 日期类型,day今日,month本月
     */
    public List<OrderRankVo> querySaleNumberTop10(String dayType) {
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate localDate = LocalDate.now();

        String startTime;
        // 今日
        if (DayTypeConstant.DAY_TYPE_DAY.equals(dayType)) {
            startTime = fmt.format(localDate.minusDays(1)) + " 00:00:00";
        } else {
            // 本月
            startTime = fmt.format(localDate.minusMonths(1)) + " 00:00:00";
        }
        String endTime = fmt.format(localDate);
        Integer payStatus = Integer.valueOf(OrderUtil.PAT_STATUS_Y);
        List<String> goodsNameList = orderStatisticsMapper.querySaleNumberTop10(startTime, endTime, payStatus);
        if (goodsNameList.isEmpty()) {
            return new ArrayList<>();
        }
        List<OrderRankVo> list = new LinkedList<>();
        OrderRankVo orderRankVo;
        for (int i = 0; i < goodsNameList.size(); i++) {
            orderRankVo=new OrderRankVo();
            orderRankVo.setGoodsName(goodsNameList.get(i));
            orderRankVo.setRank(i);
            list.add(orderRankVo);
        }
        return list;
    }

    /**
     * 销售分析
     *
     * @param dayType 日期类型,day今日,month本月
     */
    public List<OrderAnalysisVo> salesAnalysis(String dayType) {
        // 月度的销售额,按月分组
        if (DayTypeConstant.DAY_TYPE_MONTH.equals(dayType)) {
            return orderStatisticsMapper.selectMonthSaleNum(Arrays.asList("01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"),
                    Integer.valueOf(OrderUtil.PAT_STATUS_Y));
        } else {
            // 年度的销售额,按年分组
        }
        return new ArrayList<>();
    }

}
